<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.1.1">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/avatar.jpg">
  <link rel="mask-icon" href="/images/avatar.jpg" color="#222">
  <meta name="msvalidate.01" content="4AF2328B2035748153A041B7EEF3D15C">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Noto+Serif+SC:300,300italic,400,400italic,700,700italic%7CLobster:300,300italic,400,400italic,700,700italic%7CPingFang+SC:300,300italic,400,400italic,700,700italic%7CRoboto+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.33/fancybox/fancybox.css" integrity="sha256-gkQVf8UKZgQ0HyuxL/VnacadJ+D2Kox2TCEBuNQg5+w=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"ethanliu6.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":true,"sidebar":{"position":"left","display":"always","padding":12,"offset":18},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":true,"color":"#222","save":"manual"},"mediumzoom":true,"lazyload":true,"pangu":false,"comments":{"style":"buttons","active":"changyan","storage":true,"lazyload":false,"nav":null,"activeClass":"changyan"},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":6,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="该篇主要讲解Java中File类、IO流、字节流">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaSE进阶——Day10 File类、IO流、字节流">
<meta property="og:url" content="http://ethanliu6.github.io/2024/04/28/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day10/JavaSE%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94Day10-File%E7%B1%BB%E3%80%81IO%E6%B5%81/index.html">
<meta property="og:site_name" content="&lt;font color&#x3D;&#39;white&#39; &gt;Ethan の Blog&lt;&#x2F;font&gt;">
<meta property="og:description" content="该篇主要讲解Java中File类、IO流、字节流">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/EthanLiu6/ImgsForMe/blog_images/image-20240428133456984.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/EthanLiu6/ImgsForMe/blog_images/image-20240429203842812.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/EthanLiu6/ImgsForMe/blog_images/image-20240429233411482.png">
<meta property="article:published_time" content="2024-04-28T04:55:46.884Z">
<meta property="article:modified_time" content="2024-04-30T14:18:17.448Z">
<meta property="article:author" content="Ethan Liu">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="Java进阶">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/EthanLiu6/ImgsForMe/blog_images/image-20240428133456984.png">


<link rel="canonical" href="http://ethanliu6.github.io/2024/04/28/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day10/JavaSE%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94Day10-File%E7%B1%BB%E3%80%81IO%E6%B5%81/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://ethanliu6.github.io/2024/04/28/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day10/JavaSE%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94Day10-File%E7%B1%BB%E3%80%81IO%E6%B5%81/","path":"2024/04/28/JavaNotes/JavaSE进阶/day10/JavaSE进阶——Day10-File类、IO流/","title":"JavaSE进阶——Day10 File类、IO流、字节流"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>JavaSE进阶——Day10 File类、IO流、字节流 | <font color='white' >Ethan の Blog</font></title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<style>.darkmode--activated{--body-bg-color:#282828;--content-bg-color:#333;--card-bg-color:#555;--text-color:#ccc;--blockquote-color:#bbb;--link-color:#ccc;--link-hover-color:#eee;--brand-color:#ddd;--brand-hover-color:#ddd;--table-row-odd-bg-color:#282828;--table-row-hover-bg-color:#363636;--menu-item-bg-color:#555;--btn-default-bg:#222;--btn-default-color:#ccc;--btn-default-border-color:#555;--btn-default-hover-bg:#666;--btn-default-hover-color:#ccc;--btn-default-hover-border-color:#666;--highlight-background:#282b2e;--highlight-foreground:#a9b7c6;--highlight-gutter-background:#34393d;--highlight-gutter-foreground:#9ca9b6}.darkmode--activated img{opacity:.75}.darkmode--activated img:hover{opacity:.9}.darkmode--activated code{color:#69dbdc;background:0 0}button.darkmode-toggle{z-index:9999}.darkmode-ignore,img{display:flex!important}.beian img{display:inline-block!important}</style></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title"><font color='white' >Ethan の Blog</font></p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description"><font color='#FDABC1' size=3>清醒的停留，胜过——盲目的前行</font></p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-关于我"><a href="/aboutme/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">18</span></a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">14</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">53</span></a></li><li class="menu-item menu-item-love"><a href="/categories/Love/" rel="section"><i class="fas fa-heart fa-fw"></i>Love</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#file%E7%B1%BB"><span class="nav-number">1.</span> <span class="nav-text">File类</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%9B%E5%BB%BA"><span class="nav-number">1.1.</span> <span class="nav-text">创建</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%A0%E9%99%A4"><span class="nav-number">1.2.</span> <span class="nav-text">删除</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="nav-number">1.3.</span> <span class="nav-text">常用方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%83%E4%B9%A0"><span class="nav-number">1.4.</span> <span class="nav-text">练习</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#io%E6%B5%81"><span class="nav-number">2.</span> <span class="nav-text">io流</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%97%E8%8A%82%E6%B5%81%E8%BE%93%E5%87%BA%E6%B5%81"><span class="nav-number">3.</span> <span class="nav-text">字节流输出流</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%97%E8%8A%82%E8%BE%93%E5%87%BA%E6%B5%81%E5%85%A5%E9%97%A8"><span class="nav-number">3.1.</span> <span class="nav-text">字节输出流入门</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#fileoutputstream%E7%B1%BB"><span class="nav-number">3.1.1.</span> <span class="nav-text">FileOutputStream类 :</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95"><span class="nav-number">3.1.2.</span> <span class="nav-text">构造方法 :</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AD%97%E8%8A%82%E8%BE%93%E5%87%BA%E6%B5%81%E5%86%99%E6%95%B0%E6%8D%AE%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8"><span class="nav-number">3.1.3.</span> <span class="nav-text">字节输出流写数据快速入门</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%97%E8%8A%82%E8%BE%93%E5%87%BA%E6%B5%81%E5%86%99%E6%95%B0%E6%8D%AE%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-number">3.2.</span> <span class="nav-text">字节输出流写数据的方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%86%99%E6%95%B0%E6%8D%AE%E7%9A%84%E6%8D%A2%E8%A1%8C%E5%92%8C%E8%BF%BD%E5%8A%A0%E5%86%99%E5%85%A5"><span class="nav-number">3.3.</span> <span class="nav-text">写数据的换行和追加写入</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%97%E8%8A%82%E8%BE%93%E5%85%A5%E6%B5%81"><span class="nav-number">4.</span> <span class="nav-text">字节输入流</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%97%E8%8A%82%E8%BE%93%E5%85%A5%E6%B5%81%E4%BB%8B%E7%BB%8D"><span class="nav-number">4.1.</span> <span class="nav-text">字节输入流介绍</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AD%97%E8%8A%82%E8%BE%93%E5%85%A5%E6%B5%81%E7%B1%BB"><span class="nav-number">4.1.1.</span> <span class="nav-text">字节输入流类</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95-1"><span class="nav-number">4.1.2.</span> <span class="nav-text">构造方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%AD%A5%E9%AA%A4"><span class="nav-number">4.1.3.</span> <span class="nav-text">步骤</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BC%82%E5%B8%B8%E7%9A%84%E6%8D%95%E8%8E%B7%E5%A4%84%E7%90%86"><span class="nav-number">4.2.</span> <span class="nav-text">异常的捕获处理</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#jdk7%E7%89%88%E6%9C%AC%E4%B9%8B%E5%89%8D%E5%A4%84%E7%90%86%E6%96%B9%E5%BC%8F-%E6%89%8B%E5%8A%A8%E9%87%8A%E6%94%BE%E8%B5%84%E6%BA%90"><span class="nav-number">4.2.1.</span> <span class="nav-text">JDK7版本之前处理方式 :
手动释放资源</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#jdk7%E7%89%88%E6%9C%AC%E4%BC%98%E5%8C%96%E5%A4%84%E7%90%86%E6%96%B9%E5%BC%8F-%E8%87%AA%E5%8A%A8%E9%87%8A%E6%94%BE%E8%B5%84%E6%BA%90"><span class="nav-number">4.2.2.</span> <span class="nav-text">JDK7版本优化处理方式 :
自动释放资源</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BB%A3%E7%A0%81%E5%AE%9E%E8%B7%B5"><span class="nav-number">4.2.3.</span> <span class="nav-text">代码实践</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%97%E8%8A%82%E8%BE%93%E5%85%A5%E6%B5%81%E4%B8%80%E6%AC%A1%E8%AF%BB%E4%B8%80%E4%B8%AA%E5%AD%97%E8%8A%82%E6%95%B0%E7%BB%84"><span class="nav-number">4.3.</span> <span class="nav-text">字节输入流一次读一个字节数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BB%A3%E7%A0%81%E6%A1%88%E4%BE%8B"><span class="nav-number">4.4.</span> <span class="nav-text">代码案例</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%8D%95%E5%AD%97%E8%8A%82%E6%95%B0%E6%8D%AE%E8%AF%BB%E5%86%99"><span class="nav-number">4.4.1.</span> <span class="nav-text">单字节数据读写</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%97%E8%8A%82%E7%BC%93%E5%86%B2%E6%B5%81"><span class="nav-number">5.</span> <span class="nav-text">字节缓冲流</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%97%E8%8A%82%E7%BC%93%E5%86%B2%E6%B5%81%E6%A6%82%E8%BF%B0"><span class="nav-number">5.1.</span> <span class="nav-text">字节缓冲流概述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%9B%E5%BB%BA-1"><span class="nav-number">5.2.</span> <span class="nav-text">创建：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B"><span class="nav-number">5.3.</span> <span class="nav-text">案例：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#properties%E9%9B%86%E5%90%88%E7%B1%BB%E9%87%8D%E7%82%B9"><span class="nav-number">6.</span> <span class="nav-text">Properties集合类「重点！！！」</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A6%82%E8%BF%B0"><span class="nav-number">6.1.</span> <span class="nav-text">概述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="nav-number">6.2.</span> <span class="nav-text">基本使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#properties%E4%BD%9C%E4%B8%BA%E9%9B%86%E5%90%88%E7%9A%84%E7%89%B9%E6%9C%89%E6%96%B9%E6%B3%95"><span class="nav-number">6.3.</span> <span class="nav-text">Properties作为集合的特有方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#properties%E4%B8%AD%E5%92%8Cio%E7%9B%B8%E5%85%B3%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-number">6.4.</span> <span class="nav-text">properties中和IO相关的方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%A1%A5%E5%85%85"><span class="nav-number">6.5.</span> <span class="nav-text">补充</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%80%92%E5%BD%92"><span class="nav-number">7.</span> <span class="nav-text">递归</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E9%80%92%E5%BD%92"><span class="nav-number">7.1.</span> <span class="nav-text">什么是递归？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%80%92%E5%BD%92%E6%A1%88%E4%BE%8B"><span class="nav-number">7.2.</span> <span class="nav-text">递归案例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%80%92%E5%BD%92%E7%9A%84%E5%BC%8A%E7%AB%AF"><span class="nav-number">7.3.</span> <span class="nav-text">递归的弊端：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%BC%E5%90%88%E6%A1%88%E4%BE%8B"><span class="nav-number">7.4.</span> <span class="nav-text">综合案例</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Ethan Liu"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Ethan Liu</p>
  <div class="site-description" itemprop="description"><font color='#0000FF' size=1.5px>语言学 | NLP | Py | Java</font></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">53</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2V0aGFubGl1Ng==" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;ethanliu6"><i class="fab fa-github fa-fw"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="bWFpbHRvOmxpdWUyMzM2QGdtYWlsLmNvbQ==" title="E-Mail → mailto:liue2336@gmail.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9zcGFjZS5iaWxpYmlsaS5jb20vMTMyNzA5OTk3Nw==" title="BiliBili → https:&#x2F;&#x2F;space.bilibili.com&#x2F;1327099977"><i class="fa-brands fa-bilibili fa-fw"></i>BiliBili</span>
      </span>
      <span class="links-of-author-item">
        <a href="/categories/Love/" title="Love → &#x2F;categories&#x2F;Love&#x2F;" rel="noopener me"><i class="fa fa-heart fa-fw"></i>Love</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://ethanliu6.github.io/2024/04/28/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day10/JavaSE%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94Day10-File%E7%B1%BB%E3%80%81IO%E6%B5%81/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Ethan Liu">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="<font color='white' >Ethan の Blog</font>">
      <meta itemprop="description" content="<font color='#0000FF' size=1.5px>语言学 | NLP | Py | Java</font>">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="JavaSE进阶——Day10 File类、IO流、字节流 | <font color='white' >Ethan の Blog</font>">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JavaSE进阶——Day10 File类、IO流、字节流<span class="exturl post-edit-link" data-url="aHR0cHM6Ly9naXRodWIuY29tL0V0aGFuTGl1Ni9ldGhhbmxpdTYuZ2l0aHViLmlvL3RyZWUvbWFzdGVyL19wb3N0cy9KYXZhTm90ZXMvSmF2YVNF6L+b6Zi2L2RheTEwL0phdmFTRei/m+mYtuKAlOKAlERheTEwLUZpbGXnsbvjgIFJT+a1gS5tZA==" title="编辑"><i class="fa fa-pen-nib"></i></span>
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2024-04-28 12:55:46" itemprop="dateCreated datePublished" datetime="2024-04-28T12:55:46+08:00">2024-04-28</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2024-04-30 22:18:17" itemprop="dateModified" datetime="2024-04-30T22:18:17+08:00">2024-04-30</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java%E5%85%A8%E6%A0%88/" itemprop="url" rel="index"><span itemprop="name">Java全栈</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    <a title="JavaSE进阶——Day10 File类、IO流、字节流" href="/2024/04/28/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day10/JavaSE%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94Day10-File%E7%B1%BB%E3%80%81IO%E6%B5%81/#SOHUCS" itemprop="discussionUrl">
      <span id="sourceId::757a55c67eff382f0fd1acb5735c6bf0" class="cy_cmt_count" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>2.2k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>8 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><blockquote class="blockquote-center">
<p>该篇主要讲解Java中<strong>File类、IO流、字节流</strong></p>

</blockquote>
<span id="more"></span>
<h2 id="file类">File类</h2>
<p>什么是File类？</p>
<ul>
<li>java.io.File类是java语言提供了用来描述 磁盘文件 和磁盘目录 的</li>
</ul>
<h3 id="创建">创建</h3>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="title function_">File</span><span class="params">(String path)</span></span><br><span class="line">    </span><br><span class="line"><span class="comment">//文件对象</span></span><br><span class="line">    <span class="type">File</span>  <span class="variable">file</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;F:\JavaSE进阶_day10\01_课件\小记.txt&quot;</span>);</span><br><span class="line">    </span><br><span class="line"><span class="comment">//目录对象</span></span><br><span class="line">    <span class="type">File</span>  <span class="variable">dir</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;F:\JavaSE进阶_day10\01_课件&quot;</span>);</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="title function_">File</span><span class="params">(String parent , String child)</span><span class="comment">//基于父路径 和子路径 合并 后，生成File对象</span></span><br><span class="line">    </span><br><span class="line"><span class="comment">//文件对象</span></span><br><span class="line">    <span class="type">File</span>  <span class="variable">file</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;F:\JavaSE进阶_day10\01_课件&quot;</span>,<span class="string">&quot;小记.txt&quot;</span>);</span><br><span class="line">    </span><br><span class="line"><span class="comment">//目录对象</span></span><br><span class="line">    <span class="type">File</span>  <span class="variable">dir</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;F:\&quot; , &quot;</span>JavaSE进阶_day10\01_课件<span class="string">&quot;);    </span></span><br></pre></td></tr></table></figure>
<p>MacOS不太一样：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> file;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> EthanLiu 16693226842@163.com</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2024/4/28 13:03</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Project</span> JavaCode_SE_Advance</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Theme</span> File类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">demo1</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">      </span><br><span class="line">        <span class="comment">//创建File对象并创建文件夹</span></span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;./day10/src/test/&quot;</span>).mkdir(); <span class="comment">//需要mkdir()方法</span></span><br><span class="line">      </span><br><span class="line">        <span class="comment">//指定文件夹下创建文件</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">file</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;./day10/src/test/&quot;</span>, <span class="string">&quot;test.md&quot;</span>);</span><br><span class="line">        file.createNewFile(); <span class="comment">//需要createNewFile()方法</span></span><br><span class="line">      </span><br><span class="line">        System.out.println(file);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>mkdir和mkdirs就是一个可以创建父类文件，一个只能创建一个子文件</p>
<h3 id="删除">删除</h3>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">文件对象.delete(); <span class="comment">//返回值是布尔值</span></span><br><span class="line"></span><br><span class="line">注意：只能删除空目录</span><br></pre></td></tr></table></figure>
<h3 id="常用方法">常用方法</h3>
<figure>
<img data-src="https://cdn.jsdelivr.net/gh/EthanLiu6/ImgsForMe/blog_images/image-20240428133456984.png" alt="image-20240428133456984">
<figcaption aria-hidden="true">image-20240428133456984</figcaption>
</figure>
<h3 id="练习">练习</h3>
<p>统计文件个数</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> file;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"><span class="keyword">import</span> java.util.HashMap;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> EthanLiu 16693226842@163.com</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2024/4/28 15:48</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Project</span> JavaCode_SE_Advance</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Theme</span> 统计文件个数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo2</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">String</span> <span class="variable">path</span> <span class="operator">=</span> <span class="string">&quot;day10/src/resources/&quot;</span>;</span><br><span class="line">        mothed(path);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">mothed</span><span class="params">(String path)</span> &#123;</span><br><span class="line">        <span class="comment">// 利用HashMap统计个数</span></span><br><span class="line">        Map&lt;String, Integer&gt; fileMap = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;&gt;();</span><br><span class="line">        <span class="type">File</span> <span class="variable">file</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(path);</span><br><span class="line">        <span class="comment">// 判断文件夹存在</span></span><br><span class="line">        <span class="keyword">if</span> (file.exists()) &#123;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 1.创建file对应数组</span></span><br><span class="line">            File[] files = file.listFiles();</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 2.遍历文件数组并对文件格式进行提取</span></span><br><span class="line">            <span class="keyword">for</span> (File f : files) &#123;</span><br><span class="line">                <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> f.getName();<span class="comment">//获取文件名</span></span><br><span class="line">                <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> name.split(<span class="string">&quot;\\.&quot;</span>)[<span class="number">1</span>]; <span class="comment">//获取文件类型名, 对于多个.的文件名不能使用</span></span><br><span class="line">                <span class="comment">//String s = name.lastIndexOf(&quot;.&quot;)// 获取.所在的索引</span></span><br><span class="line"></span><br><span class="line">                <span class="comment">// 3.统计个数</span></span><br><span class="line">                <span class="keyword">if</span> (fileMap.get(s) == <span class="literal">null</span>)&#123;</span><br><span class="line">                    fileMap.put(s, <span class="number">1</span>);</span><br><span class="line">                &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                    <span class="type">int</span> <span class="variable">n</span> <span class="operator">=</span> fileMap.get(s);</span><br><span class="line">                    n++;</span><br><span class="line">                    fileMap.put(s, n);</span><br><span class="line"></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">&quot;输入文件路径不存在&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(fileMap);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="io流">io流</h2>
<p>IO流解决程序中的什么问题？</p>
<ul>
<li>把程序中存储在内存中的数据，写入到文件中（持久存储）</li>
<li>把磁盘文件中存储的数据，读取到内存中</li>
</ul>
<p>IO流：</p>
<ul>
<li>I流： 把磁盘文件中存储的数据，读取到内存中（读数据）</li>
<li>O流：把内存中的数据，写入到磁盘文件（写数据）</li>
</ul>
<p>InputStream：输入流</p>
<ul>
<li>字节输入流</li>
<li>字符输入流</li>
</ul>
<p>OutputStream：输出流</p>
<ul>
<li>字节输出流</li>
<li>字符输出流</li>
</ul>
<p>字节流：万能流（所有类型的文件都可以读写）</p>
<p>字符流：通常用于针对纯文本文件进行读写操作</p>
<ul>
<li>纯文本文件： 使用计算机中的记事本软件打开并看得懂的文件</li>
</ul>
<p>File类，只能针对文件本身进行创建、删除（无法对文件中存储的内容进行操作的）</p>
<p>使用IO流，对文件中存储的内容进行操作（读、写）</p>
<p>IO流中的基础流：</p>
<ul>
<li>字节流</li>
<li>字符流</li>
</ul>
<p>IO流的作用：对磁盘上的文件进行读、写操作</p>
<ul>
<li>读：输入流</li>
<li>写：输出流（把内存中的数据写入到磁盘文件中）</li>
</ul>
<h2 id="字节流输出流">字节流输出流</h2>
<h3 id="字节输出流入门">字节输出流入门</h3>
<ul>
<li><h4 id="fileoutputstream类">FileOutputStream类 :</h4>
<ul>
<li><code>OutputStream</code>有很多子类，我们从最简单的一个子类开始。</li>
<li><code>java.io.FileOutputStream</code>类是文件输出流，用于将数据写出到文件</li>
</ul></li>
<li><h4 id="构造方法">构造方法 :</h4>
<ul>
<li><code>public FileOutputStream(File file)</code>：创建文件输出流以写入由指定的
File对象表示的文件。</li>
<li><code>public FileOutputStream(String name)</code>：
创建文件输出流以指定的名称写入文件。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileOutputStreamConstructor</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">   	 	<span class="comment">// 使用File对象创建流对象</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">file</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;a.txt&quot;</span>);</span><br><span class="line">        <span class="type">FileOutputStream</span> <span class="variable">fos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(file);</span><br><span class="line">      </span><br><span class="line">        <span class="comment">// 使用文件名称创建流对象</span></span><br><span class="line">        <span class="type">FileOutputStream</span> <span class="variable">fos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;b.txt&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><h4 id="字节输出流写数据快速入门">字节输出流写数据快速入门</h4>
<ul>
<li>创建字节输出流对象。</li>
<li>写数据</li>
<li>释放资源</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.outputstream_demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.FileNotFoundException;</span><br><span class="line"><span class="keyword">import</span> java.io.FileOutputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    字节输出流写数据快速入门 :</span></span><br><span class="line"><span class="comment">        1 创建字节输出流对象。</span></span><br><span class="line"><span class="comment">        2 写数据</span></span><br><span class="line"><span class="comment">        3 释放资源</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">OutputStreamDemo1</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">// 创建字节输出流对象</span></span><br><span class="line">        <span class="comment">// 如果指定的文件不存在 , 会自动创建文件</span></span><br><span class="line">        <span class="comment">// 如果文件存在 , 会把文件中的内容清空</span></span><br><span class="line">        <span class="type">FileOutputStream</span> <span class="variable">fos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;day11_demo\\a.txt&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 写数据</span></span><br><span class="line">        <span class="comment">// 写到文件中就是以字节形式存在的</span></span><br><span class="line">        <span class="comment">// 只是文件帮我们把字节翻译成了对应的字符 , 方便查看</span></span><br><span class="line">        fos.write(<span class="number">97</span>);</span><br><span class="line">        fos.write(<span class="number">98</span>);</span><br><span class="line">        fos.write(<span class="number">99</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 释放资源</span></span><br><span class="line">        <span class="comment">// while(true)&#123;&#125;</span></span><br><span class="line">        <span class="comment">// 断开流与文件中间的关系</span></span><br><span class="line">        fos.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="字节输出流写数据的方法">字节输出流写数据的方法</h3>
<ul>
<li><p>字节流写数据的方法</p>
<ul>
<li>1 void write(int b) 一次写一个字节数据</li>
<li>2 void write(byte[] b) 一次写一个字节数组数据</li>
<li>3 void write(byte[] b, int off, int len)
一次写一个字节数组的部分数据</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.outputstream_demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.FileNotFoundException;</span><br><span class="line"><span class="keyword">import</span> java.io.FileOutputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    字节流写数据的3种方式</span></span><br><span class="line"><span class="comment">        1 void write​(int b)	一次写一个字节数据</span></span><br><span class="line"><span class="comment">        2 void write​(byte[] b)	一次写一个字节数组数据</span></span><br><span class="line"><span class="comment">        3 void write​(byte[] b, int off, int len)	一次写一个字节数组的部分数据</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">OutputStreamDemo2</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">// 创建字节输出流对象</span></span><br><span class="line">        <span class="type">FileOutputStream</span> <span class="variable">fos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;day11_demo\\a.txt&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 写数据</span></span><br><span class="line"><span class="comment">//        1 void write​(int b)	一次写一个字节数据</span></span><br><span class="line">        fos.write(<span class="number">97</span>);</span><br><span class="line">        fos.write(<span class="number">98</span>);</span><br><span class="line">        fos.write(<span class="number">99</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        2 void write​(byte[] b)	一次写一个字节数组数据</span></span><br><span class="line">        <span class="type">byte</span>[] bys = &#123;<span class="number">65</span>, <span class="number">66</span>, <span class="number">67</span>, <span class="number">68</span>, <span class="number">69</span>&#125;;</span><br><span class="line">        fos.write(bys);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        3 void write​(byte[] b, int off, int len)	一次写一个字节数组的部分数据</span></span><br><span class="line">        fos.write(bys, <span class="number">0</span>, <span class="number">3</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 释放资源</span></span><br><span class="line">        fos.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="写数据的换行和追加写入">写数据的换行和追加写入</h3>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">package com.itheima.outputstream_demo;</span><br><span class="line"></span><br><span class="line">import java.io.FileOutputStream;</span><br><span class="line">import java.io.IOException;</span><br><span class="line"></span><br><span class="line">/*</span><br><span class="line">    字节流写数据的换行和追加写入</span><br><span class="line"></span><br><span class="line">    1 字节流写数据如何实现换行呢？</span><br><span class="line">        写完数据后，加换行符</span><br><span class="line">        windows : \r\n</span><br><span class="line">        linux : \n</span><br><span class="line">        mac : \r</span><br><span class="line"></span><br><span class="line">    2 字节流写数据如何实现追加写入呢？</span><br><span class="line">        通过构造方法 : public FileOutputStream(String name，boolean append)</span><br><span class="line">        创建文件输出流以指定的名称写入文件。如果第二个参数为true ，不会清空文件里面的内容</span><br><span class="line"> */</span><br><span class="line">public class OutputStreamDemo3 &#123;</span><br><span class="line">    public static void main(String[] args) throws IOException &#123;</span><br><span class="line">        // 创建字节输出流对象</span><br><span class="line">        FileOutputStream fos = new FileOutputStream(&quot;day11_demo\\a.txt&quot;);</span><br><span class="line"></span><br><span class="line">        // void write(int b)  一次写一个字节数据</span><br><span class="line">        fos.write(97);</span><br><span class="line">        // 因为字节流无法写入一个字符串 , 把字符串转成字节数组写入</span><br><span class="line">        fos.write(&quot;\r\n&quot;.getBytes());</span><br><span class="line">        fos.write(98);</span><br><span class="line">        fos.write(&quot;\r\n&quot;.getBytes());</span><br><span class="line">        fos.write(99);</span><br><span class="line">        fos.write(&quot;\r\n&quot;.getBytes());</span><br><span class="line"></span><br><span class="line">        // 释放资源</span><br><span class="line">        fos.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.outputstream_demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.FileOutputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    字节流写数据的换行和追加写入</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    1 字节流写数据如何实现换行呢？</span></span><br><span class="line"><span class="comment">        写完数据后，加换行符</span></span><br><span class="line"><span class="comment">        windows : \r\n</span></span><br><span class="line"><span class="comment">        linux : \n</span></span><br><span class="line"><span class="comment">        mac : \r</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    2 字节流写数据如何实现追加写入呢？</span></span><br><span class="line"><span class="comment">        通过构造方法 : public FileOutputStream​(String name，boolean append)</span></span><br><span class="line"><span class="comment">        创建文件输出流以指定的名称写入文件。如果第二个参数为true ，不会清空文件里面的内容</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">OutputStreamDemo3</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">// 创建字节输出流对象</span></span><br><span class="line">        <span class="comment">// 追加写数据</span></span><br><span class="line">        <span class="comment">// 通过构造方法 : public FileOutputStream​(String name，boolean append) : 追加写数据</span></span><br><span class="line">        <span class="type">FileOutputStream</span> <span class="variable">fos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;day11_demo\\a.txt&quot;</span> , <span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// void write​(int b)	一次写一个字节数据</span></span><br><span class="line">        fos.write(<span class="number">97</span>);</span><br><span class="line">        <span class="comment">// 因为字节流无法写入一个字符串 , 把字符串转成字节数组写入</span></span><br><span class="line">        fos.write(<span class="string">&quot;\r\n&quot;</span>.getBytes());</span><br><span class="line">        fos.write(<span class="number">98</span>);</span><br><span class="line">        fos.write(<span class="string">&quot;\r\n&quot;</span>.getBytes());</span><br><span class="line">        fos.write(<span class="number">99</span>);</span><br><span class="line">        fos.write(<span class="string">&quot;\r\n&quot;</span>.getBytes());</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 释放资源</span></span><br><span class="line">        fos.close();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 写完数据换行操作</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method1</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">// 创建字节输出流对象</span></span><br><span class="line">        <span class="type">FileOutputStream</span> <span class="variable">fos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;day11_demo\\a.txt&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// void write​(int b)	一次写一个字节数据</span></span><br><span class="line">        fos.write(<span class="number">97</span>);</span><br><span class="line">        <span class="comment">// 因为字节流无法写入一个字符串 , 把字符串转成字节数组写入</span></span><br><span class="line">        fos.write(<span class="string">&quot;\r\n&quot;</span>.getBytes());</span><br><span class="line">        fos.write(<span class="number">98</span>);</span><br><span class="line">        fos.write(<span class="string">&quot;\r\n&quot;</span>.getBytes());</span><br><span class="line">        fos.write(<span class="number">99</span>);</span><br><span class="line">        fos.write(<span class="string">&quot;\r\n&quot;</span>.getBytes());</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 释放资源</span></span><br><span class="line">        fos.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="字节输入流">字节输入流</h2>
<h3 id="字节输入流介绍">字节输入流介绍</h3>
<h4 id="字节输入流类">字节输入流类</h4>
<ul>
<li>InputStream类 : 字节输入流最顶层的类 , 抽象类 --- FileInputStream类
: FileInputStream extends InputStream</li>
</ul>
<h4 id="构造方法-1">构造方法</h4>
<ul>
<li>public FileInputStream(File file) : 从file类型的路径中读取数据</li>
<li>public FileInputStream(String name) : 从字符串路径中读取数据</li>
</ul>
<h4 id="步骤">步骤</h4>
<ul>
<li><p>创建输入流对象</p></li>
<li><p>读数据</p></li>
<li><p>释放资源</p></li>
<li><p>``` package com.itheima.inputstream_demo;</p>
<p>import java.io.FileInputStream; import java.io.IOException;</p>
<p>/<em> 字节输入流写数据快速入门 : 一次读一个字节 第一部分 :
字节输入流类 InputStream类 : 字节输入流最顶层的类 , 抽象类 ---
FileInputStream类 : FileInputStream extends InputStream 第二部分 :
构造方法 public FileInputStream(File file) : 从file类型的路径中读取数据
public FileInputStream(String name) : 从字符串路径中读取数据 第三部分 :
字节输入流步骤 1 创建输入流对象 2 读数据 3 释放资源 </em>/ public class
FileInputStreamDemo1 { public static void main(String[] args) throws
IOException { // 创建字节输入流对象 // 读取的文件必须存在 , 不存在则报错
FileInputStream fis = new FileInputStream("day11_demo\a.txt");</p>
<pre><code>    // 读数据 , 从文件中读到一个字节
    // 返回的是一个int类型的字节
    // 如果想看字符, 需要强转
    int by = fis.read();
    System.out.println((char) by);

    // 释放资源
    fis.close();
&#125;</code></pre>
<p>} <figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">###  字节输入流读多个字节</span><br><span class="line"></span><br></pre></td></tr></table></figure> package com.itheima.inputstream_demo;</p></li>
</ul>
<p>import java.io.FileInputStream; import java.io.IOException;</p>
<p>/<em> 字节输入流写数据快速入门 : 读多个字节 第一部分 : 字节输入流类
InputStream类 : 字节输入流最顶层的类 , 抽象类 --- FileInputStream类 :
FileInputStream extends InputStream 第二部分 : 构造方法 public
FileInputStream(File file) : 从file类型的路径中读取数据 public
FileInputStream(String name) : 从字符串路径中读取数据 第三部分 :
字节输入流步骤 1 创建输入流对象 2 读数据 3 释放资源 </em>/ public class
FileInputStreamDemo2 { public static void main(String[] args) throws
IOException { // 创建字节输入流对象 // 读取的文件必须存在 , 不存在则报错
FileInputStream fis = new FileInputStream("day11_demo\a.txt");</p>
<pre><code>    // 读数据 , 从文件中读到一个字节
    // 返回的是一个int类型的字节
    // 如果想看字符, 需要强转</code></pre>
<p>// int by = fis.read(); // System.out.println(by); // by =
fis.read(); // System.out.println(by); // by = fis.read(); //
System.out.println(by); // // by = fis.read(); //
System.out.println(by); // by = fis.read(); // System.out.println(by);
// by = fis.read(); // System.out.println(by);</p>
<pre><code>    // 循环改进
    int by;// 记录每次读到的字节
    while ((by = fis.read()) != -1) &#123;
        System.out.print((char) by);
    &#125;

    // 释放资源
    fis.close();
&#125;</code></pre>
<p>} <figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">###  图片的拷贝</span><br><span class="line"></span><br><span class="line">```java</span><br><span class="line">package com.itheima.inputstream_demo;</span><br><span class="line"></span><br><span class="line">import java.io.FileInputStream;</span><br><span class="line">import java.io.FileNotFoundException;</span><br><span class="line">import java.io.FileOutputStream;</span><br><span class="line">import java.io.IOException;</span><br><span class="line"></span><br><span class="line">/*</span><br><span class="line">    需求 : 把 &quot;图片路径\xxx.jpg&quot; 复制到当前模块下</span><br><span class="line"></span><br><span class="line">    分析：</span><br><span class="line">        复制文件，其实就把文件的内容从一个文件中读取出来(数据源)，然后写入到另一个文件中(目的地)</span><br><span class="line">        数据源：</span><br><span class="line">            xxx.jpg  --- 读数据 --- FileInputStream</span><br><span class="line">        目的地：</span><br><span class="line">            模块名称\copy.jpg --- 写数据 --- FileOutputStream</span><br><span class="line"></span><br><span class="line"> */</span><br><span class="line">public class FileInputStreamDemo2 &#123;</span><br><span class="line">    public static void main(String[] args) throws IOException &#123;</span><br><span class="line">        // 创建字节输入流对象</span><br><span class="line">        FileInputStream fis = new FileInputStream(&quot;D:\\传智播客\\安装包\\好看的图片\\liqin.jpg&quot;);</span><br><span class="line"></span><br><span class="line">        // 创建字节输出流</span><br><span class="line">        FileOutputStream fos = new FileOutputStream(&quot;day11_demo\\copy.jpg&quot;);</span><br><span class="line"></span><br><span class="line">        // 一次读写一个字节</span><br><span class="line">        int by;</span><br><span class="line">        while ((by = fis.read()) != -1) &#123;</span><br><span class="line">            fos.write(by);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        // 释放资源</span><br><span class="line">        fis.close();</span><br><span class="line">        fos.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></p>
<h3 id="异常的捕获处理">异常的捕获处理</h3>
<ul>
<li><h4 id="jdk7版本之前处理方式-手动释放资源">JDK7版本之前处理方式 :
手动释放资源</h4>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.inputstream_demo;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.FileInputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.FileOutputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    需求 : 对上一个赋值图片的代码进行使用捕获方式处理</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileInputStreamDemo4</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">FileInputStream</span> <span class="variable">fis</span> <span class="operator">=</span> <span class="literal">null</span> ;</span><br><span class="line">        <span class="type">FileOutputStream</span> <span class="variable">fos</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">// 创建字节输入流对象</span></span><br><span class="line">            fis = <span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;D:\\传智播客\\安装包\\好看的图片\\liqin.jpg&quot;</span>);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 创建字节输出流</span></span><br><span class="line">            fos = <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;day11_demo\\copy.jpg&quot;</span>);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 一次读写一个字节</span></span><br><span class="line">            <span class="type">int</span> by;</span><br><span class="line">            <span class="keyword">while</span> ((by = fis.read()) != -<span class="number">1</span>) &#123;</span><br><span class="line">                fos.write(by);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            <span class="comment">// 释放资源</span></span><br><span class="line">            <span class="keyword">if</span>(fis != <span class="literal">null</span>)&#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    fis.close();</span><br><span class="line">                &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 释放资源</span></span><br><span class="line">            <span class="keyword">if</span>(fos != <span class="literal">null</span>)&#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    fos.close();</span><br><span class="line">                &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><h4 id="jdk7版本优化处理方式-自动释放资源">JDK7版本优化处理方式 :
自动释放资源</h4>
<ul>
<li><p>JDK7优化后可以使用 try-with-resource 语句 ,
该语句确保了每个资源在语句结束时自动关闭。 简单理解 :
使用此语句,会自动释放资源 , 不需要自己在写finally代码块了</p></li>
<li><p>格式 :</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">格式 :  </span><br><span class="line"><span class="keyword">try</span> (创建流对象语句<span class="number">1</span> ; 创建流对象语句<span class="number">2</span> ...) &#123;</span><br><span class="line">        <span class="comment">// 读写数据</span></span><br><span class="line">    &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">        处理异常的代码...</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">举例 :</span><br><span class="line">    <span class="keyword">try</span> ( </span><br><span class="line">        <span class="type">FileInputStream</span> <span class="variable">fis1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;day11_demo\\a.txt&quot;</span>) ; </span><br><span class="line">	    <span class="type">FileInputStream</span> <span class="variable">fis2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;day11_demo\\b.txt&quot;</span>) ) </span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">// 读写数据</span></span><br><span class="line">    &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">        处理异常的代码...</span><br><span class="line">    &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul></li>
<li><h4 id="代码实践">代码实践</h4>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.inputstream_demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.FileInputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.FileOutputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    JDK7版本优化处理方式</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    需求 : 对上一个赋值图片的代码进行使用捕获方式处理</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileInputStreamDemo5</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="keyword">try</span> (</span><br><span class="line">                <span class="comment">// 创建字节输入流对象</span></span><br><span class="line">                <span class="type">FileInputStream</span> <span class="variable">fis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;D:\\传智播客\\安装包\\好看的图片\\liqin.jpg&quot;</span>);</span><br><span class="line">                <span class="comment">// 创建字节输出流</span></span><br><span class="line">                <span class="type">FileOutputStream</span> <span class="variable">fos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;day11_demo\\copy.jpg&quot;</span>)</span><br><span class="line">        ) &#123;</span><br><span class="line">            <span class="comment">// 一次读写一个字节</span></span><br><span class="line">            <span class="type">int</span> by;</span><br><span class="line">            <span class="keyword">while</span> ((by = fis.read()) != -<span class="number">1</span>) &#123;</span><br><span class="line">                fos.write(by);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 释放资源 , 发现已经灰色 , 提示多余的代码 , 所以使用 try-with-resource 方式会自动关流</span></span><br><span class="line">            <span class="comment">// fis.close();</span></span><br><span class="line">            <span class="comment">// fos.close();</span></span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="字节输入流一次读一个字节数组">字节输入流一次读一个字节数组</h3>
<ul>
<li><p>FileInputStream类 :</p>
<ul>
<li>public int read(byte[] b) : 从输入流读取最多b.length个字节的数据,
返回的是真实读到的数据个数</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.inputstream_demo;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.sound.midi.Soundbank;</span><br><span class="line"><span class="keyword">import</span> java.io.FileInputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.FileNotFoundException;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">   FileInputStream类 :</span></span><br><span class="line"><span class="comment">        public int read​(byte[] b)：</span></span><br><span class="line"><span class="comment">        1 从输入流读取最多b.length个字节的数据</span></span><br><span class="line"><span class="comment">        2 返回的是真实读到的数据个数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileInputStreamDemo6</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">// 创建字节输入流对象</span></span><br><span class="line">        <span class="type">FileInputStream</span> <span class="variable">fis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;day11_demo\\a.txt&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        public int read​(byte[] b)：</span></span><br><span class="line"><span class="comment">//        1 从输入流读取最多b.length个字节的数据</span></span><br><span class="line"><span class="comment">//        2 返回的是真实读到的数据个数</span></span><br><span class="line"></span><br><span class="line">        <span class="type">byte</span>[] bys = <span class="keyword">new</span> <span class="title class_">byte</span>[<span class="number">3</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">//        int len = fis.read(bys);</span></span><br><span class="line"><span class="comment">//        System.out.println(len);// 3</span></span><br><span class="line"><span class="comment">//        System.out.println(new String(bys));// abc</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//        len = fis.read(bys);</span></span><br><span class="line"><span class="comment">//        System.out.println(len);// 2</span></span><br><span class="line"><span class="comment">//        System.out.println(new String(bys));// efc</span></span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;==========代码改进===============&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        int len = fis.read(bys);</span></span><br><span class="line"><span class="comment">//        System.out.println(len);// 3</span></span><br><span class="line"><span class="comment">//        System.out.println(new String(bys, 0, len));// abc</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//        len = fis.read(bys);</span></span><br><span class="line"><span class="comment">//        System.out.println(len);// 2</span></span><br><span class="line"><span class="comment">//        System.out.println(new String(bys, 0, len));// ef</span></span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;==========代码改进===============&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">int</span> len;</span><br><span class="line">        <span class="keyword">while</span> ((len = fis.read(bys)) != -<span class="number">1</span>) &#123;</span><br><span class="line">            System.out.print(<span class="keyword">new</span> <span class="title class_">String</span>(bys , <span class="number">0</span> , len));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        fis.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>对复制图片的代码进行使用一次读写一个字节数组的方式进行改进</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.inputstream_demo;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.FileInputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.FileNotFoundException;</span><br><span class="line"><span class="keyword">import</span> java.io.FileOutputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    需求 : 对复制图片的代码进行使用一次读写一个字节数组的方式进行改进</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    FileInputStream类 :</span></span><br><span class="line"><span class="comment">        public int read​(byte[] b)：</span></span><br><span class="line"><span class="comment">        1 从输入流读取最多b.length个字节的数据</span></span><br><span class="line"><span class="comment">        2 返回的是真实读到的数据个数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileInputStreamDemo7</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">// 创建字节输入流对象</span></span><br><span class="line">        <span class="type">FileInputStream</span> <span class="variable">fis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;D:\\传智播客\\安装包\\好看的图片\\liqin.jpg&quot;</span>);</span><br><span class="line">        <span class="comment">// 创建字节输出流</span></span><br><span class="line">        <span class="type">FileOutputStream</span> <span class="variable">fos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;day11_demo\\copy.jpg&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">byte</span>[] bys = <span class="keyword">new</span> <span class="title class_">byte</span>[<span class="number">1024</span>];</span><br><span class="line">        <span class="type">int</span> len;<span class="comment">// 每次真实读到数据的个数</span></span><br><span class="line">        <span class="type">int</span> by;</span><br><span class="line">        <span class="keyword">while</span> ((len = fis.read(bys)) != -<span class="number">1</span>) &#123;</span><br><span class="line">            fos.write(bys, <span class="number">0</span>, len);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 释放资源</span></span><br><span class="line">        fis.close();</span><br><span class="line">        fos.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="代码案例">代码案例</h3>
<h4 id="单字节数据读写">单字节数据读写</h4>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.FileInputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.FileOutputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> EthanLiu 16693226842@163.com</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2024/4/28 21:15</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Project</span> JavaCode_SE_Advance</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Theme</span> 单字节数据读写</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">CopyFile</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line"></span><br><span class="line">        <span class="type">String</span> <span class="variable">srcFile</span> <span class="operator">=</span> <span class="string">&quot;day10/src/resources/222.jpeg&quot;</span>; <span class="comment">//源数据文件</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">destFile</span> <span class="operator">=</span> <span class="string">&quot;day10/src/demo/111.jpeg&quot;</span>; <span class="comment">//目标文件</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//计时</span></span><br><span class="line">        <span class="type">long</span> <span class="variable">startTime</span> <span class="operator">=</span> System.currentTimeMillis();</span><br><span class="line">        copy(srcFile, destFile);</span><br><span class="line">        <span class="type">long</span> <span class="variable">endTile</span> <span class="operator">=</span> System.currentTimeMillis();</span><br><span class="line">        System.out.println(<span class="string">&quot;复制花费了&quot;</span> + (endTile - startTime) + <span class="string">&quot;毫秒&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> srcFile 源数据文件地址</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> destFile 目标文件地址</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">copy</span><span class="params">(String srcFile, String destFile)</span> &#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//创建输入输出流</span></span><br><span class="line">        <span class="comment">//循环读取</span></span><br><span class="line">        <span class="comment">//循环写入</span></span><br><span class="line">        <span class="keyword">try</span>( <span class="type">FileInputStream</span> <span class="variable">fis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileInputStream</span>(srcFile);</span><br><span class="line">             <span class="type">FileOutputStream</span> <span class="variable">fos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(destFile))&#123; <span class="comment">//升级: 省略手动释放资源</span></span><br><span class="line"></span><br><span class="line">            <span class="type">int</span> <span class="variable">data</span> <span class="operator">=</span> -<span class="number">1</span>;</span><br><span class="line">            <span class="comment">//未读完</span></span><br><span class="line">            <span class="keyword">while</span> ((data = fis.read()) != -<span class="number">1</span>)&#123;</span><br><span class="line">                fos.write(data); <span class="comment">//读一个字节数据写一个字节数据</span></span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125;<span class="keyword">catch</span> (IOException e)&#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.FileInputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.FileOutputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> EthanLiu 16693226842@163.com</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2024/4/28 21:50</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Project</span> JavaCode_SE_Advance</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Theme</span> 多字节数据读写</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">CopyFile2</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line"></span><br><span class="line">        <span class="type">String</span> <span class="variable">srcFile</span> <span class="operator">=</span> <span class="string">&quot;day10/src/resources/222.jpeg&quot;</span>; <span class="comment">//源数据文件</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">destFile</span> <span class="operator">=</span> <span class="string">&quot;day10/src/demo/demo2.txt&quot;</span>; <span class="comment">//目标文件</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//计时</span></span><br><span class="line">        <span class="type">long</span> <span class="variable">startTime</span> <span class="operator">=</span> System.currentTimeMillis();</span><br><span class="line">        copy(srcFile, destFile);</span><br><span class="line">        <span class="type">long</span> <span class="variable">endTile</span> <span class="operator">=</span> System.currentTimeMillis();</span><br><span class="line">        System.out.println(<span class="string">&quot;复制花费了&quot;</span> + (endTile - startTime) + <span class="string">&quot;毫秒&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">copy</span><span class="params">(String srcFile, String destFile)</span> &#123;</span><br><span class="line">        <span class="keyword">try</span>(<span class="type">FileInputStream</span> <span class="variable">fis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileInputStream</span>(srcFile); <span class="comment">//输入字节流</span></span><br><span class="line">            <span class="type">FileOutputStream</span> <span class="variable">fos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(destFile))&#123; <span class="comment">//输出字节流</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">//字节数组</span></span><br><span class="line">            <span class="type">byte</span>[] buf = <span class="keyword">new</span> <span class="title class_">byte</span>[<span class="number">1024</span>]; <span class="comment">//一次读取1k</span></span><br><span class="line">            <span class="type">int</span> <span class="variable">len</span> <span class="operator">=</span> -<span class="number">1</span>;</span><br><span class="line">            <span class="keyword">while</span> ((len = fis.read(buf)) != -<span class="number">1</span>)&#123;</span><br><span class="line">                fos.write(buf, <span class="number">0</span>, len);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">        &#125;<span class="keyword">catch</span> (IOException e)&#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="字节缓冲流">字节缓冲流</h2>
<div class="note primary"><p>在IO体系下，java提供了高效流（提高读写文件的效率）</p>
</div>
<h3 id="字节缓冲流概述">字节缓冲流概述</h3>
<ul>
<li>字节缓冲流：
<ul>
<li>​ BufferOutputStream：缓冲输出流</li>
<li>​ BufferedInputStream：缓冲输入流</li>
</ul></li>
<li>构造方法：
<ul>
<li>​ 字节缓冲输出流：BufferedOutputStream(OutputStream out)</li>
<li>​ 字节缓冲输入流：BufferedInputStream(InputStream in)</li>
</ul></li>
<li>为什么构造方法需要的是字节流，而不是具体的文件或者路径呢？
<ul>
<li><font color="red"> 字节缓冲流仅仅提供缓冲区，不具备读写功能 ,
而真正的读写数据还得依靠基本的字节流对象进行操作</font></li>
</ul></li>
</ul>
<p>在IO体系下，java提供了高效流（提高读写文件的效率）</p>
<ul>
<li>读文件：BufferedInputStream</li>
<li>写文件：BufferedOutputStream</li>
</ul>
<hr>
<h3 id="创建-1">创建：</h3>
<p>在IO流中基础流只有两个：字节流、字符流</p>
<p>BufferedInputStream流的创建： 读数据</p>
<ul>
<li>自己没有读数据的能力，需要依赖字节输入流实现读数据</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//构造方法： public  BufferedInputStream( InputStream  is )</span></span><br><span class="line"></span><br><span class="line"><span class="type">BufferedInputStream</span> <span class="variable">bis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedInputStream</span>( <span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;关联文件&quot;</span>) );</span><br></pre></td></tr></table></figure>
<p>BufferedOutputStream流的创建： 写数据</p>
<ul>
<li>自己没有写数据的能力，需要依赖字节输出流实现读数据</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//构造方法： public  BufferedOutputStream( OutputStream  os )</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//覆盖写入</span></span><br><span class="line"><span class="type">BufferedOutputStream</span> <span class="variable">bos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedOutputStream</span>( <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;关联文件&quot;</span>) );</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//追加写入 （构造方法的第二个参数为true）</span></span><br><span class="line"><span class="type">FileOutputStream</span> <span class="variable">fos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;关联文件&quot;</span> , <span class="literal">true</span>); <span class="comment">//追加写入</span></span><br><span class="line"><span class="type">BufferedOutputStream</span> <span class="variable">bos</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedOutputStream</span>(  fos );<span class="comment">//当前流有具有追加写入能力</span></span><br></pre></td></tr></table></figure>
<figure>
<img data-src="https://cdn.jsdelivr.net/gh/EthanLiu6/ImgsForMe/blog_images/image-20240429203842812.png" alt="image-20240429203842812">
<figcaption aria-hidden="true">image-20240429203842812</figcaption>
</figure>
<h3 id="案例">案例：</h3>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> buffered;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.*;</span><br><span class="line"><span class="keyword">import</span> java.nio.file.FileVisitOption;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> EthanLiu 16693226842@163.com</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2024/4/29 19:54</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Project</span> JavaCode_SE_Advance</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Theme</span> 使用字节缓冲流进行文件复制</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">CopyFile1</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">String</span> <span class="variable">srcFile</span> <span class="operator">=</span> <span class="string">&quot;day10/src/buffered/TwoAnimations.mp4&quot;</span>; <span class="comment">//原文件</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">destFile</span> <span class="operator">=</span> <span class="string">&quot;day10/src/buffered/CopyTwoAnimations.mp4&quot;</span>; <span class="comment">//目标文件</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//计时</span></span><br><span class="line">        <span class="type">long</span> <span class="variable">startTime</span> <span class="operator">=</span> System.currentTimeMillis();</span><br><span class="line">        <span class="comment">//文件复制</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            copy(srcFile, destFile);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//耗时</span></span><br><span class="line">        <span class="type">long</span> <span class="variable">endTime</span> <span class="operator">=</span> System.currentTimeMillis();</span><br><span class="line">        System.out.println(<span class="string">&quot;总耗时&quot;</span> + (endTime - startTime) + <span class="string">&quot;毫秒&quot;</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> srcFile Sting类型的源文件</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> destFile Sting类型的目标文件</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">copy</span><span class="params">(String srcFile, String destFile)</span>&#123;</span><br><span class="line">        <span class="comment">//读数据</span></span><br><span class="line">        <span class="comment">//创建字节输入流</span></span><br><span class="line">        <span class="comment">// 写数据</span></span><br><span class="line">        <span class="comment">//创建字节输出流</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">//使用升级后的jdk功能</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">//使用字节缓冲流</span></span><br><span class="line">            <span class="type">BufferedInputStream</span> <span class="variable">bfis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedInputStream</span>( <span class="keyword">new</span> <span class="title class_">FileInputStream</span>(srcFile));</span><br><span class="line">            <span class="type">BufferedOutputStream</span> <span class="variable">bfos</span>  <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedOutputStream</span>( <span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(destFile, <span class="literal">true</span>));  <span class="comment">//追加写入</span></span><br><span class="line">            <span class="comment">//2.循环读取</span></span><br><span class="line">            <span class="type">byte</span>[] datas = <span class="keyword">new</span> <span class="title class_">byte</span>[<span class="number">1024</span>]; <span class="comment">//一次读取数据大小 1k</span></span><br><span class="line">            <span class="type">int</span> <span class="variable">len</span> <span class="operator">=</span> -<span class="number">1</span>; <span class="comment">//初始化读取数据</span></span><br><span class="line">            <span class="comment">// 判断是否读完,没完,写入</span></span><br><span class="line">            <span class="keyword">while</span> ((len = bfis.read(datas)) != -<span class="number">1</span>) &#123; <span class="comment">//为了方便写入数据所以初始化了data</span></span><br><span class="line">                bfos.write(datas, <span class="number">0</span>, len);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;<span class="keyword">catch</span> (IOException e)&#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="properties集合类重点"><font color="red">Properties集合类「重点！！！」</font></h2>
<h3 id="概述">概述</h3>
<div class="note primary"><ul>
<li><p>properties是一个Map体系的集合类</p>
<ul>
<li><code>public class Properties extends Hashtable &lt;Object,Object&gt;</code></li>
</ul></li>
<li><p>为什么在IO流部分学习Properties</p>
<ul>
<li>Properties中有跟IO相关的方法</li>
</ul></li>
<li><p>当做双列集合使用</p>
<ul>
<li>不需要加泛型 , 工作中只存字符串</li>
</ul></li>
</ul>
</div>
<h3 id="基本使用">基本使用</h3>
<p>Properties类：</p>
<ol type="1">
<li>作为Map集合的子类存在（存储的数据：key、value）</li>
<li>可以结合IO流使用 （读、写）</li>
</ol>
<p>Properties类的基本使用：</p>
<ol type="1">
<li>作为集合使用</li>
<li>结合IO流使用</li>
</ol>
<p>构造方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">Properties</span> <span class="variable">prop</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Properties</span>();</span><br></pre></td></tr></table></figure>
<p>常用方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//向Properties集合中存储：key、value</span></span><br><span class="line">setPorperty(String key , String value)</span><br><span class="line"></span><br><span class="line"><span class="comment">//根据指定的key，获取Properties集合中对应的value    </span></span><br><span class="line">String <span class="title function_">getProperty</span><span class="params">(String key)</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">//获取Properties集合中所有的key元素    </span></span><br><span class="line">Set&lt;String&gt;  <span class="title function_">stringPropertyNames</span><span class="params">()</span>;   </span><br><span class="line"></span><br><span class="line"><span class="comment">//借用字节输入流，读取配置文件，读取配置文件中的数据会存储到Properties集合中</span></span><br><span class="line"><span class="comment">//配置文件中的书写格式：key=value</span></span><br><span class="line">load(InputStream  is )</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.properties_demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"><span class="keyword">import</span> java.util.Properties;</span><br><span class="line"><span class="keyword">import</span> java.util.Set;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    1 properties是一个Map体系的集合类</span></span><br><span class="line"><span class="comment">        - `public class Properties extends Hashtable &lt;Object,Object&gt;`</span></span><br><span class="line"><span class="comment">    2 为什么在IO流部分学习Properties</span></span><br><span class="line"><span class="comment">        - Properties中有跟IO相关的方法</span></span><br><span class="line"><span class="comment">    3 当做双列集合使用</span></span><br><span class="line"><span class="comment">        - 不需要加泛型 , 工作中只存字符串</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PropertiesDemo1</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// 创建集合对象</span></span><br><span class="line">        <span class="type">Properties</span> <span class="variable">properties</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Properties</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 添加元素</span></span><br><span class="line">        properties.put(<span class="string">&quot;it001&quot;</span> , <span class="string">&quot;张三&quot;</span>);</span><br><span class="line">        properties.put(<span class="string">&quot;it002&quot;</span> , <span class="string">&quot;李四&quot;</span>);</span><br><span class="line">        properties.put(<span class="string">&quot;it003&quot;</span> , <span class="string">&quot;王五&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 遍历集合 : 键找值</span></span><br><span class="line">        Set&lt;Object&gt; set = properties.keySet();</span><br><span class="line">        <span class="keyword">for</span> (Object key : set) &#123;</span><br><span class="line">            System.out.println(key + <span class="string">&quot;---&quot;</span> + properties.get(key));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;========================&quot;</span>);</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 遍历集合 : 获取对对象集合 , 获取键和值</span></span><br><span class="line">        Set&lt;Map.Entry&lt;Object, Object&gt;&gt; set2 = properties.entrySet();</span><br><span class="line">        <span class="keyword">for</span> (Map.Entry&lt;Object, Object&gt; entry : set2) &#123;</span><br><span class="line">            <span class="type">Object</span> <span class="variable">key</span> <span class="operator">=</span> entry.getKey();</span><br><span class="line">            <span class="type">Object</span> <span class="variable">value</span> <span class="operator">=</span> entry.getValue();</span><br><span class="line">            System.out.println(key + <span class="string">&quot;---&quot;</span> + value);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="properties作为集合的特有方法">Properties作为集合的特有方法</h3>
<ul>
<li>Object setProperty(String key, String value)
设置集合的键和值，都是String类型，相当于put方法</li>
<li>String getProperty(String key) 使用此属性列表中指定的键搜索属性 ,
相当于get方法</li>
<li>Set<String> stringPropertyNames()
从该属性列表中返回一个不可修改的键集，其中键及其对应的值是字符串 ,
相当于keySet方法</String></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.properties_demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Properties;</span><br><span class="line"><span class="keyword">import</span> java.util.Set;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    Properties作为集合的特有方法</span></span><br><span class="line"><span class="comment">        Object setProperty(String key, String value)	设置集合的键和值，都是String类型，相当于put方法</span></span><br><span class="line"><span class="comment">        String getProperty(String key)	使用此属性列表中指定的键搜索属性 , 相当于get方法</span></span><br><span class="line"><span class="comment">        Set&lt;String&gt; stringPropertyNames​()	从该属性列表中返回一个不可修改的键集，其中键及其对应的值是字符串 , 相当于keySet方法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PropertiesDemo2</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// 创建集合对象</span></span><br><span class="line">        <span class="type">Properties</span> <span class="variable">properties</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Properties</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 添加元素</span></span><br><span class="line">        properties.setProperty(<span class="string">&quot;it001&quot;</span>, <span class="string">&quot;张三&quot;</span>);</span><br><span class="line">        properties.setProperty(<span class="string">&quot;it002&quot;</span>, <span class="string">&quot;李四&quot;</span>);</span><br><span class="line">        properties.setProperty(<span class="string">&quot;it003&quot;</span>, <span class="string">&quot;王五&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 遍历集合 : 键找值</span></span><br><span class="line">        Set&lt;String&gt; set = properties.stringPropertyNames();</span><br><span class="line">        <span class="keyword">for</span> (String key : set) &#123;</span><br><span class="line">            System.out.println(key + <span class="string">&quot;---&quot;</span> + properties.getProperty(key));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="properties中和io相关的方法">properties中和IO相关的方法</h3>
<ul>
<li>void load(InputStream inStream) 以字节流形式 , 把文件中的键值对,
读取到集合中</li>
<li>void load(Reader reader) 以字符流形式 , 把文件中的键值对,
读取到集合中</li>
<li>void store(OutputStream out, String comments)
把集合中的键值对,以字节流形式写入文件中 , 参数二为注释</li>
<li>void store(Writer writer, String comments)
把集合中的键值对,以字符流形式写入文件中 , 参数二为注释</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.properties_demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.FileInputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.util.Properties;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">   Properties和IO流结合的方法</span></span><br><span class="line"><span class="comment">        void load​(InputStream inStream)	以字节流形式 , 把文件中的键值对, 读取到集合中</span></span><br><span class="line"><span class="comment">        //void load​(Reader reader)	以字符流形式 , 把文件中的键值对, 读取到集合中</span></span><br><span class="line"><span class="comment">        void store​(OutputStream out, String comments)	把集合中的键值对,以字节流形式写入文件中 , 参数二为注释</span></span><br><span class="line"><span class="comment">        //void store​(Writer writer, String comments)	把集合中的键值对,以字符流形式写入文件中 , 参数二为注释</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PropertiesDemo3</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">// 创建Properties集合对象</span></span><br><span class="line">        <span class="type">Properties</span> <span class="variable">properties</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Properties</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// void load​(InputStream inStream)	以字节流形式 , 把文件中的键值对, 读取到集合中</span></span><br><span class="line">        properties.load(<span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;day11_demo\\prop.properties&quot;</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 打印集合中的数据</span></span><br><span class="line">        System.out.println(properties);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.itheima.properties_demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.FileInputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.FileOutputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.util.Properties;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">   Properties和IO流结合的方法</span></span><br><span class="line"><span class="comment">        void load​(InputStream inStream)	以字节流形式 , 把文件中的键值对, 读取到集合中</span></span><br><span class="line"><span class="comment">        //void load​(Reader reader)	以字符流形式 , 把文件中的键值对, 读取到集合中</span></span><br><span class="line"><span class="comment">        void store​(OutputStream out, String comments)	把集合中的键值对,以字节流形式写入文件中 , 参数二为注释</span></span><br><span class="line"><span class="comment">        //void store​(Writer writer, String comments)	把集合中的键值对,以字符流形式写入文件中 , 参数二为注释</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PropertiesDemo3</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">Properties</span> <span class="variable">properties</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Properties</span>();</span><br><span class="line">        properties.setProperty(<span class="string">&quot;zhangsan&quot;</span> , <span class="string">&quot;23&quot;</span>);</span><br><span class="line">        properties.setProperty(<span class="string">&quot;lisi&quot;</span> , <span class="string">&quot;24&quot;</span>);</span><br><span class="line">        properties.setProperty(<span class="string">&quot;wangwu&quot;</span> , <span class="string">&quot;25&quot;</span>);</span><br><span class="line">        properties.store(<span class="keyword">new</span> <span class="title class_">FileOutputStream</span>(<span class="string">&quot;day11_demo\\prop2.properties&quot;</span>) , <span class="string">&quot;userMessage&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method1</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">// 创建Properties集合对象</span></span><br><span class="line">        <span class="type">Properties</span> <span class="variable">properties</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Properties</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// void load​(InputStream inStream)	以字节流形式 , 把文件中的键值对, 读取到集合中</span></span><br><span class="line">        properties.load(<span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;day11_demo\\prop.properties&quot;</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 打印集合中的数据</span></span><br><span class="line">        System.out.println(properties);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="补充">补充</h3>
<p>在开发中，通常会使用到配置文件，而配置文件格式通常分为：</p>
<ol type="1">
<li>XML文件</li>
<li>properties文件</li>
<li>....</li>
</ol>
<hr>
<p>Properties类的作用：</p>
<ul>
<li>读取开发中使用的到.properties配置文件
<ul>
<li>properties配置文件中的数据是以key/value形式体现</li>
<li>把properties配置文件中的key，存储到Properties类中的key元素下</li>
<li>把properties配置文件中的value，存储到Properties类中的value元素下</li>
</ul></li>
</ul>
<h2 id="递归">递归</h2>
<h3 id="什么是递归">什么是递归？</h3>
<ul>
<li>递归是应用于方法上的解决方案：方法自己调用自己</li>
</ul>
<p>递归 = 递进 + 回归</p>
<ul>
<li>递进 ：前进</li>
<li>回归： 后退</li>
</ul>
<h3 id="递归案例">递归案例</h3>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">package digui;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * @Author EthanLiu 16693226842@163.com</span><br><span class="line"> * @Date 2024/4/29 23:26</span><br><span class="line"> * @Project JavaCode_SE_Advance</span><br><span class="line"> * @Theme 递归案例</span><br><span class="line"> */</span><br><span class="line">public class Demo1 &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        int x = 16;</span><br><span class="line">        System.out.println(factorial(x));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 用于计算整数阶乘</span><br><span class="line">     * @param x 整数x</span><br><span class="line">     * @return x的阶乘</span><br><span class="line">     */</span><br><span class="line">    private static int factorial(int x) &#123;</span><br><span class="line">        if (x == 1 || x == 0)&#123;</span><br><span class="line">            return 1;</span><br><span class="line">        &#125;</span><br><span class="line">        return x * factorial(x - 1);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure>
<img data-src="https://cdn.jsdelivr.net/gh/EthanLiu6/ImgsForMe/blog_images/image-20240429233411482.png" alt="image-20240429233411482">
<figcaption aria-hidden="true">image-20240429233411482</figcaption>
</figure>
<h3 id="递归的弊端">递归的弊端：</h3>
<ul>
<li><p>当递归调用的方法过多时，会造成栈内存的溢出</p></li>
<li><p>解决方案： 队列结构</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">1. 获取所有的File对象，存储到集合中</span><br><span class="line">2. 遍历集合</span><br><span class="line">   判断：是文件</span><br><span class="line">        判断：是否为java文件</span><br><span class="line">   判断：是目录</span><br><span class="line">        存储到集合中   </span><br><span class="line">  </span><br><span class="line">   细节： 从集合中获取一个数据后，该数据在集合中就无用了   </span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="综合案例">综合案例</h3>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> digui;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> EthanLiu 16693226842@163.com</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2024/4/29 23:52</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Project</span> JavaCode_SE_Advance</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Theme</span> 综合案例: 打印给定文件夹下的所有java或者Python文件</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo2</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//定义文件输入流</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">strFile</span> <span class="operator">=</span> <span class="string">&quot;/users/ethan.liu/desktop/&quot;</span>;</span><br><span class="line">        <span class="type">File</span> <span class="variable">file</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(strFile);</span><br><span class="line"></span><br><span class="line">        String[] types = &#123;<span class="string">&quot;ppt&quot;</span>, <span class="string">&quot;java&quot;</span>, <span class="string">&quot;py&quot;</span>, <span class="string">&quot;docx&quot;</span>, <span class="string">&quot;zip&quot;</span>&#125;;</span><br><span class="line">        getSearchFile(file, types);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/***</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> file 给定指定文件夹</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> types 指定查询文件类型</span></span><br><span class="line"><span class="comment">     *              (以.结尾的文件扩展名</span></span><br><span class="line"><span class="comment">     *              eg:&quot;py&quot;)</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">getSearchFile</span><span class="params">(File file, String[] types)</span> &#123;</span><br><span class="line">        <span class="comment">//给定文件夹不存在, 抛出异常</span></span><br><span class="line">        <span class="keyword">if</span> (!file.exists()) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">&quot;输入文件路径不存在&quot;</span>);</span><br><span class="line">        &#125;<span class="comment">//存在,查找</span></span><br><span class="line">        <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">//获取文件元素列表</span></span><br><span class="line">            File[] files = file.listFiles();</span><br><span class="line">            <span class="comment">//判断是文件还是文件夹</span></span><br><span class="line">            <span class="keyword">for</span> (File f : files) &#123;</span><br><span class="line">                <span class="keyword">if</span> (f.isFile()) &#123; <span class="comment">//是文件, 查询</span></span><br><span class="line">                    <span class="keyword">for</span> (String type : types) &#123;</span><br><span class="line">                        <span class="keyword">if</span> (f.getName().endsWith(<span class="string">&quot;.&quot;</span> + type)) &#123; <span class="comment">//判断文件类型是否为指定文件类型</span></span><br><span class="line">                            System.out.println(f.getAbsoluteFile()); <span class="comment">//获取指定文件类型的绝对路径</span></span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125; <span class="keyword">else</span> <span class="keyword">if</span> (f.isDirectory()) &#123; <span class="comment">//是文件夹, 递归调用方法</span></span><br><span class="line">                    getSearchFile(f, types);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="reward-container">
  <div>请我一杯咖啡吧！</div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wChat.png" alt="Ethan Liu 微信">
        <span>微信</span>
      </div>

  </div>
</div>

          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Ethan Liu
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="http://ethanliu6.github.io/2024/04/28/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day10/JavaSE%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94Day10-File%E7%B1%BB%E3%80%81IO%E6%B5%81/" title="JavaSE进阶——Day10 File类、IO流、字节流">http://ethanliu6.github.io/2024/04/28/JavaNotes/JavaSE进阶/day10/JavaSE进阶——Day10-File类、IO流/</a>
  </li>
  <li class="post-copyright-license">
      <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC8="><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


          <div class="post-tags">
              <a href="/tags/Java/" rel="tag"><i class="fa fa-tag"></i> Java</a>
              <a href="/tags/Java%E8%BF%9B%E9%98%B6/" rel="tag"><i class="fa fa-tag"></i> Java进阶</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2024/04/27/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day09/JavaSE%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94Day09%E5%A4%9A%E7%BA%BF%E7%A8%8B%EF%BC%88%E4%B8%80%EF%BC%89/" rel="prev" title="JavaSE进阶——Day09 多线程（一）">
                  <i class="fa fa-angle-left"></i> JavaSE进阶——Day09 多线程（一）
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2024/04/28/love/%E4%B8%AA%E4%BA%BA%E6%97%A5%E8%AE%B024-04-28/" rel="next" title="个人日记24-04-28">
                  个人日记24-04-28 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="SOHUCS" sid="757a55c67eff382f0fd1acb5735c6bf0"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2023-2024 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Is Ethan's</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">113k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">6:51</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL0V0aGFuTGl1Ni8=" title="在 GitHub 上关注我" aria-label="在 GitHub 上关注我"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.33/fancybox/fancybox.umd.js" integrity="sha256-+2+qOqR8CKoHh/AsVR9k2qaDBKWjYNC2nozhYmv5j9k=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/medium-zoom/1.1.0/medium-zoom.min.js" integrity="sha256-ZgMyDAIYDYGxbcpJcfUnYwNevG/xi9OHKaR/8GK+jWc=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>

  <script class="next-config" data-name="pdf" type="application/json">{"object_url":{"url":"https://cdnjs.cloudflare.com/ajax/libs/pdfobject/2.2.12/pdfobject.min.js","integrity":"sha256-g2xji1rlE3KsGVClvuxTbcR0Kn2+wtQADSff2Tbb4zA="},"url":"/lib/pdf/web/viewer.html"}</script>
  <script src="/js/third-party/tags/pdf.js"></script>



  <script src="/js/third-party/fancybox.js"></script>



  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"all","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


  <script src="https://cdnjs.cloudflare.com/ajax/libs/quicklink/2.3.0/quicklink.umd.js" integrity="sha256-yvJQOINiH9fWemHn0vCA5lsHWJaHs6/ZmO+1Ft04SvM=" crossorigin="anonymous"></script>
  <script class="next-config" data-name="quicklink" type="application/json">{"enable":true,"home":true,"archive":true,"delay":true,"timeout":3000,"priority":true,"url":"http://ethanliu6.github.io/2024/04/28/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day10/JavaSE%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94Day10-File%E7%B1%BB%E3%80%81IO%E6%B5%81/"}</script>
  <script src="/js/third-party/quicklink.js"></script>
<script class="next-config" data-name="changyan" type="application/json">{"enable":true,"appid":"cyxf6tytI","appkey":"90987f30c4b5248baad5a04c7a6abfe1","count":true}</script>
<script src="/js/third-party/comments/changyan.js"></script>

</body>
</html>
